﻿using EA.BA;
using EA.Common;
using EA.DTO;
using EAWebAdmin.template;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EAAdmin
{
    public partial class ViewProduct : WebPage
    {
        override
        protected void Load(object sender, EventArgs e)
        {
            int page = Request["page"]==null?1:Convert.ToInt32(Request["page"]);
            ProductBA productBA = new ProductBA();
            List<ProductTO> listProduct= productBA.GetAllProduct(page);
            Attributes.Add("listProduct", listProduct);
            Attributes.Add("totalProduct", productBA.GetTotalAllProduct());
            Attributes.Add("page",page);
        }

        public void Delete(HttpRequest Request, HttpResponse Response)
        {

            if (Request["id"] != null && !"".Equals(Request["id"]) && StringUtil.IsPositiveNumber(Request["id"]))
            {
                CustomerBA customerBA = new CustomerBA();
                int ID = Convert.ToInt32(Request["id"]);
                string email = "nhoxinh24289@gmail.com";
                string password = "daothihongnhung";
                CustomerTO customerTO = customerBA.GetCustomerByProductID(ID);

                if (customerTO == null)
                {
                    this.error("Product is invalid");
                    this.isLoad = true;
                    return;
                }
                String address = customerTO.Email;
                if (address == null || "".Equals(address) || !StringUtil.IsValidEmail(address))
                {
                    this.isLoad = true;
                    this.error("Address is invalid");
                }
                else
                {
                    String subject = "Delete Product";
                    String content = "Your product has already deleted by admin";
                    ProductBA productBA = new ProductBA();
                    Boolean stt = productBA.DeleteProduct(ID);
                    if (stt)
                    {
                        Attributes.Add("success", "Delete successfully");      
                        SendMail.SendEmail(email, password, address, subject, content);
                        this.isLoad = true;
                    }
                    else
                    {
                        Attributes.Add("err", "Delete faulty");
                        this.isLoad = true;
                    }
                }
            }
        }
    }
}